<?php


namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;
use think\Session;

class Shouyi extends Common
{
    public function index()
    {
        $where = ['reg'=>1];
        
        $admin = Session::get('admin', 'mmjtAdmin');
        $type = db('admin_type')->where('id', $admin['type'])->value('title');
        if ($type == '城市代理') {
            if (substr($admin['city'], -1) == '市') {
                $admin['city'] = substr($admin['city'], 0, mb_strlen($admin['city']) - 1);
            }
            $where['city'] = ['like', $admin['city'] . '%'];
        }
        
        $type = input('type');
        if ($type == 1) {
            $where['vip_end_datetime'] = ['egt', date('Y-m-d H:i:s')];
        } else if ($type == 2) {
            $where['is_identity_authentication'] = '2';
            $where['sex'] = "女";
        } else if ($type == 3) {
            $where['register_datetime'] = ['egt', date('Y-m-d')];
        } else if ($type == 4) {
            $where['sex'] = "男";
        } else if ($type == 5) {
            $where['sex'] = "女";
        }


        $search_name = input('param.name');
        $search_type = input('param.type');
        if ($search_name && $search_type) {
            if ($search_type == 'shangji') {
                $yqm = Db::name('user')->where(['id' => $search_name])->value('yqm');
                $where['yqr'] = $yqm;
            } else {
                $where[$search_type] = ['like', '%' . $search_name . '%'];
            }
        }
        
        $data = Db::name('user')->where($where)->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);
        
        $this->assign('type', $type);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    public function add()
    {
        $id = input('id');
        
        if (request()->isAjax()) {
            
            $post = input('post.');

            if ($id) {//修改数据
    
                foreach ($post as $k => $v) {
                    $post[$k] = $v / 100;
                }
                $db = Db::name('user')->where('id', $id)->update($post);
                if ($db !== false) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('user')->where('id', $id)->find();
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }
    
    public function records()
    {
        $where = [];
        $content = input('content/s', '');
        $start = input('start');
        $end = input('end');
        
        $admin = Session::get('admin', 'mmjtAdmin');
        $type = db('admin_type')->where('id', $admin['type'])->value('title');
        if ($type == '城市代理') {
            if (substr($admin['city'], -1) == '市') {
                $admin['city'] = substr($admin['city'], 0, mb_strlen($admin['city']) - 1);
            }
            $where['city'] = ['like', $admin['city'] . '%'];
        }

        $toward = input('toward');
        if ($toward !== null) {
            $where['t1.data'] = $toward;
        }

        $order = input('order/d', 2);//默认按照收支时间正序排列
        $this->assign('order', $order);
        $ordersort = ['t1.id desc', 't1.time asc'];
        
        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            if($search_type == 'touid'){
                $where['t1.' . $search_type] = $search_name;
            }elseif($search_type == 'id'){
                $where['t1.uid'] = $search_name;
            }else{
                $where['t2.' . $search_type] = $search_name;
            }
        }
        
        if (!$search_name) {
            //$start = $start ?: date('Y-m-d', strtotime('-7 day')) . ' 00:00:00';
        }

        if ($start && $end) {
            $where['t1.time'] = ['between', [strtotime($start), strtotime($end)]];
            $start_id = _day_first_id($start, 0, 'yaoqing_records', 'time', 'id');
            $end_id = _day_first_id(date('Y-m-d', strtotime($start))==date('Y-m-d', strtotime($end))?date('Y-m-d', strtotime($end)+86400):$end, $start_id, 'yaoqing_records', 'time', 'id');
            if ($end_id) {
                $where['t1.id'] = ['between', [$start_id, $end_id]];
            }
        } else if ($start && !$end) {
            $where['t1.time'] = ['egt', strtotime($start)];
            $start_id = _day_first_id($start, 0, 'yaoqing_records', 'time', 'id');
            if ($start_id) {
                $where['t1.id'] = ['egt', $start_id];
            }
        } else if (!$start && $end) {
            $where['t1.time'] = ['elt', strtotime($end)];
            $end_id = _day_first_id($end, 0, 'yaoqing_records', 'time', 'id');
            if ($end_id) {
                $where['t1.id'] = ['egt', $end_id];
            }
        }

        if ($content) {
            $where['t1.content'] = ['like', "%$content%"];
        }
        
        $data = Db::name('yaoqing_records t1')
            ->field('t1.*, t2.name, t2.phone')
            ->join('user t2', 't1.uid = t2.id', 'inner')
            ->where($where)->order($ordersort[$order - 1])
            ->paginate(_getCachePages(), false, ['query' => request()->param()])
            ->each(function ($item) {
                if(stripos($item['content'], '语音通话收益') !== false || stripos($item['content'], '视频通话收益') !== false && $item['order_id']){
                    $tmp = Db::name('order_common')->cache(60)->find($item['order_id']);
                    if($tmp){
                        $tmpdata = json_decode($tmp['data'], true);
                        $item['content'] .= ' ' . ($tmpdata['duration']?:1) . '分钟';
                    }
                }
                return $item;
            });

        $sum = Db::name('yaoqing_records t1')->join('user t2', 't1.uid = t2.id', 'inner')->where($where)->sum('if(t1.data, -abs(t1.money), t1.money)');

        $atypeid=$admin['type'];
        //echo $atypeid;
        if($atypeid==7 && !$search_name){//法律顾问 不检索不显示变动金额
            $sum=0;
            $data=array();
            $this->assign('sum', round($sum, 2));
            $this->assign('data', $data);
            $page='';
            $this->assign('page', $page);
        }else{
            $this->assign('data', $data);
            $page = $data->render();
            $this->assign('page', $page);
        }

        $this->assign('start', $start);
        $this->assign('end', $end);
        $this->assign('toward', $toward);
        $this->assign('content', $content);

        $this->assign('sum', round($sum, 2));
        return $this->fetch();
    }
}